package de.geckocraft.GeckoEventBroadcaster.Main;

import java.util.ArrayList;
import java.util.Date;

import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

public class AutoBroadcast
{
	private static boolean	caststartet	= false;

	public static void autocast()
	{
		
		long random = Math.round(Math.random()*199) +1;
		@SuppressWarnings("unused")
		BukkitTask task = new checkcast(Main.getMain()).runTaskLater(Main.getPlugin(), random);
	}

	private static class checkcast extends BukkitRunnable
	{

		@SuppressWarnings("unused")
		private Main	plugin;

		private checkcast(Main plugin)
		{
			this.plugin = plugin;
		}

		public void run()
		{
			int intervallstart = MainConfig.getIntervallstart();
			int intervall = MainConfig.getintervall();
			int intervallstop = MainConfig.getintervallstop();

			if (!EventList.Eventdate.isEmpty())
			{

				Date eventdate = EventList.Eventdate.get(EventList.Eventnummbers.get(0));

				Date now = new Date();
				int nowsek = (int) (now.getTime() / 1000);

				if (eventdate.getTime() < now.getTime())
				{
					EventList.removeEvent(EventList.Eventnummbers.get(0));
					@SuppressWarnings("unused")
					BukkitTask task = new checkcast(Main.getMain()).runTaskLater(Main.getPlugin(), 0);
				}
				else
				{
					if ((eventdate.getTime() / 1000) - (intervallstop * 60) > nowsek)
					{
						int nextbroadcast = -1;

						int i = 1;
						int calsek = (int) (eventdate.getTime() / 1000);

						while (calsek > nowsek)
						{
							nextbroadcast = calsek - nowsek;
							calsek = (int) ((eventdate.getTime() / 1000) - (i * intervall * 60));
							if (i * intervall > intervallstart)
							{
								calsek = nowsek + 1;
								nextbroadcast = -1;
							}
							i++;
						}
						if (nextbroadcast > 0)
						{
							if (nextbroadcast < 11)
							{
								if (!caststartet)
								{
									@SuppressWarnings("unused")
									BukkitTask task = new cast(Main.getMain()).runTaskLater(Main.getPlugin(), nextbroadcast * 20);
									caststartet = true;
								}
							}
						}
					}
					else
					{
						ArrayList<Integer> zusatzList = MainConfig.getZusatzList();
						for (int i = 0; i < zusatzList.size(); i++)
						{
							int zusatzintervall = zusatzList.get(i);

							int calsek = (int) ((eventdate.getTime() / 1000) - (zusatzintervall * 60));

							if (calsek > nowsek)
							{
								int nextbroadcast = calsek - nowsek;
								if (nextbroadcast < 11)
								{
									if (!caststartet)
									{
										@SuppressWarnings("unused")
										BukkitTask task = new cast(Main.getMain()).runTaskLater(Main.getPlugin(), nextbroadcast * 20);
										caststartet = true;
									}
									i = zusatzList.size();
								}
								else
								{
									i = zusatzList.size();
								}
							}
						}
					}
					autocast();
				}
			}
			else
			{
				autocast();
			}
		}
	}

	private static class cast extends BukkitRunnable
	{

		@SuppressWarnings("unused")
		private Main	plugin;

		private cast(Main plugin)
		{
			this.plugin = plugin;
		}

		@SuppressWarnings("deprecation")
		public void run()
		{
			caststartet = false;
			
			Date eventdate = EventList.Eventdate.get(EventList.Eventnummbers.get(0));
			String eventname = EventList.Eventname.get(EventList.Eventnummbers.get(0));
			Date now = new Date();
			long diferensSek = (eventdate.getTime() - now.getTime()) / 1000;
			long stundenSek = eventdate.getHours() * 60 * 60;
			long minutenSek = eventdate.getMinutes() * 60;
			long diferensDaySek = diferensSek - stundenSek - minutenSek;

			if (diferensDaySek < 0)
			{
				if (diferensSek * -1 > MainConfig.getchangetext())
				{
					String time = "" + eventdate.getHours() + ":" + eventdate.getMinutes();
					Bukkit.getServer().broadcastMessage(Template.parseMessage("today", time, eventname));
				}
				else
				{

					long stundenchach = (diferensSek + 1) / 60 / 60;
					long minutenchach = (diferensSek + 1) / 60;
					long diferensDaySekchach = (diferensSek + 1);

					while (diferensDaySekchach >= (1 * 60 * 60))
					{
						diferensDaySekchach = diferensDaySekchach - 60 * 60;
						minutenchach = diferensDaySekchach / 60;
					}

					String stunden = "" + stundenchach;
					String min = "" + minutenchach;

					if (minutenchach == 0)
					{

						Bukkit.getServer().broadcastMessage(Template.parseMessage("todayh", stunden, eventname));
					}
					else if (stundenchach == 0)
					{

						Bukkit.getServer().broadcastMessage(Template.parseMessage("todaymin", min, eventname));
					}
					else
					{
						Bukkit.getServer().broadcastMessage(Template.parseMessage("todayhmin", stunden, min, eventname));
					}
				}
			}
			else if (diferensDaySek < 86400)
			{
				String time = "" + eventdate.getHours() + ":" + eventdate.getMinutes();
				Bukkit.getServer().broadcastMessage(Template.parseMessage("today", time, eventname));
			}
			else
			{
				String datum = "" + eventdate.getDay() + "." + eventdate.getMonth() + "." + eventdate.getYear();
				String time = "" + eventdate.getHours() + ":" + eventdate.getMinutes();
				Bukkit.getServer().broadcastMessage(Template.parseMessage("longer", datum, time, eventname));
			}
		}
	}
}
